function goBack() {
	if (history.length==0) {
		window.close();
	} else {
		history.go(-1);
	}
}

function showMessage(MessageID) {
	if (Message[MessageID]) {
		alert(Message[MessageID]);
	} else {
		alert(MessageID);
	}
}
function checkHkid(sHKIDNum, sHKIDCheck){

    var iCheckSum;
    var iCheckDigit;
    var sCheckDigit;

    var sHKIDAlphabet2;
    var sHKIDNumber6;
    var i ;
    sHKIDNum = sHKIDNum + sHKIDCheck;
    check_flag = false  ;
    iCheckSum = 0 ;
    iCheckDigit = 0 ;
    sCheckDigit = "" ;

   
    if (sHKIDNum.length == 8 ) {
      sHKIDNum = " " + sHKIDNum;
    }    
    sHKIDAlphabet2 = sHKIDNum.substr(0, 2)
    sHKIDNumber6 = sHKIDNum.substr(2, 6)

    
    if ((sHKIDNumber6 == sHKIDNumber6 - 0) && sHKIDNum.length == 9){
      if (sHKIDAlphabet2.charAt(0) == " " ){
        iCheckDigit = 36;
      } else {
        temp = sHKIDAlphabet2.charAt(0);
        iCheckDigit = temp.toUpperCase().charCodeAt(0) - 55;
      }

      iCheckSum = iCheckDigit * 9
      temp = sHKIDAlphabet2.charAt(1);
      iCheckDigit = temp.toUpperCase().charCodeAt(0) - 55;
      iCheckSum = iCheckSum + iCheckDigit * 8

      for (i = 1; i <= 6; i++) {
        iCheckDigit = sHKIDNumber6.substr(i - 1, 1)
        iCheckSum = iCheckSum + iCheckDigit * (8 - i)
      }

      iCheckSum = 11 - iCheckSum % 11;

      if (iCheckSum < 10){
        sCheckDigit = iCheckSum.toString();
      } else {
        if (iCheckSum == 10){
          sCheckDigit = "A";
        } else {
          sCheckDigit = "0";
        }
      }

      if (sCheckDigit == sHKIDNum.substr(8, 1)){
        check_flag = true;
      } else {
        check_flag = false;
      }

      return check_flag;
    } else {
      return check_flag;
    }
}


function IsBlank(asValue) {
  if (TrimString(asValue) == "") {
    return true;
  }
  return false;
}



function CheckEmail(asEmail) {
  if (IsBlank(asEmail))
    return false;
  else if (asEmail.indexOf(" ") >= 0)
    return false;
  else if (asEmail.indexOf("@") < 0)
    return false;
  else if (asEmail.indexOf(".") < 0)
    return false;
  else if (asEmail.indexOf("'") >= 0)
    return false;
    
	var emailFilter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!(emailFilter.test(asEmail))) { 
	       return false;
	}
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (asEmail.match(illegalChars)) {
	   return false;
	
	}
  return true;
}
function DateCompare(asDate1, asDate2)
{

	

  if (TrimString(asDate1).length != 10)
    return false;
  if (asDate1.charAt(2) != '-' || asDate1.charAt(5) != '-')
    return false;
  if (TrimString(asDate2).length != 10)
    return false;
  if (asDate2.charAt(2) != '-' || asDate2.charAt(5) != '-')
    return false;


   var date1 = asDate1.substring(3, 5)+"-"+asDate1.substring(0, 2)+"-"+asDate1.substring(6, 10);
   var date2 = asDate2.substring(3, 5)+"-"+asDate2.substring(0, 2)+"-"+asDate2.substring(6, 10);
   return DateCompareMMDDYYYY(date1, date2);
}



function DateCompareMMDDYYYY(asDate1, asDate2) {

  var month;
  if(asDate1.substring(0,1)=='0')
    month = asDate1.substring(1,2);
  else
    month = asDate1.substring(0,2);
  var lsDate1 = asDate1.substring(3,5) + "-" + MonthTransform(parseInt(month)) + "-" + asDate1.substring(6);
  if(asDate2.substring(0,1)=='0')
    month = asDate2.substring(1,2);
  else
    month = asDate2.substring(0,2);
  var lsDate2 = asDate2.substring(3,5) + "-" + MonthTransform(parseInt(month)) + "-" + asDate2.substring(6);
  return DateCompareDDMONYYYY(lsDate1, lsDate2);
}



function DateCompareDDMONYYYY(asDate1, asDate2) {
  var asMM1, asDD1, asYYYY1, asMM2, asDD2, asYYYY2;
  var lsTempMM1;
  var lsTempMM2;
  var lsTempDD1;
  var lsTempDD2;
  var liDate1;
  var liDate2;
  var monthMap =
    new Array(
      "<undefine>",
      "JAN",
      "FEB",
      "MAR",
      "APR",
      "MAY",
      "JUN",
      "JUL",
      "AUG",
      "SEP",
      "OCT",
      "NOV",
      "DEC");

  monthPart = asDate1.substring(3, 6).toUpperCase();
  for (i = 1; i < monthMap.length; i++) {
    if (monthPart == monthMap[i]) {
      monthPart = i;
      break;
    }
  }
  asMM1 = monthPart;
  asDD1 = asDate1.substring(0, 2);
  asYYYY1 = asDate1.substring(7, 11);

  monthPart = asDate2.substring(3, 6).toUpperCase();
  for (i = 1; i < monthMap.length; i++) {
    if (monthPart == monthMap[i]) {
      monthPart = i;
      break;
    }
  }
  asMM2 = monthPart;
  asDD2 = asDate2.substring(0, 2);
  asYYYY2 = asDate2.substring(7, 11);

 
  lsTempMM1 = "" + (100 + parseInt(asMM1, 10));
  lsTempMM1 = lsTempMM1.substr(lsTempMM1.length - 2, lsTempMM1.length)
   
  lsTempMM2 = "" + (100 + parseInt(asMM2, 10));
  lsTempMM2 = lsTempMM2.substr(lsTempMM2.length - 2, lsTempMM2.length)
  
  lsTempDD1 = "" + (100 + parseInt(asDD1, 10));
  lsTempDD1 = lsTempDD1.substr(lsTempDD1.length - 2, lsTempDD1.length)
   
  lsTempDD2 = "" + (100 + parseInt(asDD2, 10));
  lsTempDD2 = lsTempDD2.substr(lsTempDD2.length - 2, lsTempDD2.length)
  liDate1 = asYYYY1 + lsTempMM1 + lsTempDD1
  liDate2 = asYYYY2 + lsTempMM2 + lsTempDD2
  if (parseInt(liDate1, 10) >= parseInt(liDate2, 10)) {
    return false;
  } else {
    return true;
  }
}


function MonthTransform(monthPart) {
  var monthMap =
    new Array(
      "<undefine>",
      "JAN",
      "FEB",
      "MAR",
      "APR",
      "MAY",
      "JUN",
      "JUL",
      "AUG",
      "SEP",
      "OCT",
      "NOV",
      "DEC");
  return monthMap[monthPart]
}

function CheckDateFormat(asDate) {

  if (TrimString(asDate).length != 10)
    return false;
  if (asDate.charAt(2) != '-' || asDate.charAt(5) != '-')
    return false;
  return CheckDateWithNumber(
    asDate.substring(3, 5),
    asDate.substring(0, 2),
    asDate.substring(6, 10));
}

function CheckDateWithNumber(aiMM, aiDD, aiYYYY) {
  var lsValues = "31XX31303130313130313031"


  if (!CheckNum(aiMM) || !CheckNum(aiDD) || !CheckNum(aiYYYY))
    return false;


  if ((parseInt(aiMM, 10) > 12) || (parseInt(aiMM, 10) <= 0)) {
    return false;
  }


  if (parseInt(aiYYYY, 10) < 1900) {
    return false;
  }


  var liLastDate = 0;
  if (aiMM == 2) {
    if (IsLeapYear(aiYYYY)) {
      liLastDate = 29;
    } else {
      liLastDate = 28;
    }
  } else {
    liLastDate =
      parseInt(
        lsValues.substring((aiMM - 1) * 2, (aiMM - 1) * 2 + 2),
        10);
  }

  if ((parseInt(aiDD, 10) > liLastDate) || (parseInt(aiDD, 10) <= 0)) {
    return false;
  }

  return true;
}

function IsLeapYear(asYear) {
  if (((asYear % 4) == 0)
    && ((asYear % 100) != 0)
    || ((asYear % 400) == 0)) {
    return (true);
  } else {
    return (false);
  }
}

function CheckNum(asNumber) {
  for (var i = 0; i < asNumber.length; i++) {
    var n = asNumber.charAt(i);
    if (n < '0' || n > '9')
      return false;
  }
  return true;
}

function checkAlphaNumeric(input) {
	return !input.match(/.*\W+/);
}

function checkHKIDAlphaNumberic(input){
	
	var numaric = input;
	
	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);		 
		var hh = alphaa.charCodeAt(0);
		if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==40 || hh==41)
		{
		}
		else	
		{
			 return false;
		}
	}
 return true;


}

function showConfirm(MessageID) {
	if (Message[MessageID]) {
		return confirm(Message[MessageID]);
	} else {
		return confirm(MessageID);
	}
}

function showMessages(MessageIDs) {
	var msg = "";
	for (var i = 0; i < MessageIDs.length; i++) {
		var mId = MessageIDs[i];
		var m = Message[MessageIDs[i]];
		if (m) {
			msg += (Message[MessageIDs[i]] + "\n")
		} else {
			msg += (mId + "\n")
		}
	}
	alert(msg);
}


function TrimString(str) {
  while (str.indexOf(" ") > -1) {
    str = str.replace(" ", "");
  }
  return str;
}



function RightTrim(asStr) {
  var liLen = asStr.length;
  for (var i = liLen - 1; i >= 0; i--) {
    if ((asStr.substring(i, i + 1) != " ")) {
      break;
    }
  }

  if (i < 0) {
    asStr = "";
  } else {
    asStr = asStr.substring(0, i + 1);
  }
  return asStr;
}



function OpenModalDialog(strURL,height,width) {
  var sFeatures="status:false;dialogWidth:"+width+"px;dialogHeight:"+height+"px;scrollbars=no;help=No;resizable=No;status=No"
  window.showModalDialog(strURL,strURL,sFeatures);
}


function LookupModal(formElement,strURL,height,width) {
  var sFeatures="status:false;dialogWidth:"+width+"px;dialogHeight:"+height+"px;scrollbars=no;help=No;resizable=No;status=No"
  var feedback = window.showModalDialog(strURL,strURL,sFeatures);
  if(feedback != null)
  	eval(formElement + '.value = feedback;');
}



function LookupModalWithCallback(formElement,strURL,height,width,functionName) {
  var sFeatures="status:false;dialogWidth:"+width+"px;dialogHeight:"+height+"px;scrollbars=no;help=No;resizable=No;status=No"
  var feedback = window.showModalDialog(strURL,strURL,sFeatures);
  if(feedback != null)
  {
  	eval(formElement + '.value = feedback;');
  	if (!(functionName == null || functionName == "")) {
		eval(functionName + '();');
	}
  }
}


function LookupFeedback(value) {

  window.returnValue = value;
  window.close();
}


function OpenModalDialogWithArg(strURL,a,height,width) {
  var sFeatures="status:false;dialogWidth:"+width+"px;dialogHeight:"+height+"px;scrollbars=no;help=No;resizable=No;status=No"
  window.showModalDialog(strURL,a,sFeatures);
}

function OpenModalDialogWithPara(openPage, selfPage, height, width) {
  var a = new Array;
  a[0]="";

  var sFeatures="status:false;dialogWidth:"+width+"px;dialogHeight:"+height+"px;scrollbars=no;help=No;resizable=No;status=No"
  window.showModalDialog(openPage,a,sFeatures);

  if (a[0] == "R") {
    window.location.replace(selfPage);
  }else if (a[0] == "PR") {
    
    parent.location.replace(selfPage);
  }
}
